package com.lixw.saa.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.ToolResponseMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * @ClassName: PromptController
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/10/3 11:03
 **/
@RestController
public class PromptController {

    @Resource(name = "deepseekChatModel")
    private ChatModel deepseekChatModel;
    @Resource(name = "qwenChatModel")
    private ChatModel qwenChatModel;

    @Resource(name = "deepseekChatClient")
    private ChatClient deepseekChatClient;
    @Resource(name = "qwenChatClient")
    private ChatClient qwenChatClient;

    @GetMapping("/prompt/chat")
    public Flux<String> promptChat(@RequestParam("question") String question) {
        return deepseekChatClient.prompt()
                .system("你是一个法律助手，只回答法律问题，其它问题回复，我只能回答法律相关问题，其它无可奉告")
                .user(question)
                .stream()
                .content();
    }

    @GetMapping("/prompt/chat2")
    public Flux<ChatResponse> chat2(@RequestParam("question") String question) {
        //用户消息
        UserMessage userMessage = new UserMessage(question);
        //系统消息
        SystemMessage systemMessage = new SystemMessage("你是一个讲故事的助手,每个故事控制在300字以内");
        Prompt prompt = new Prompt(userMessage, systemMessage);
        return deepseekChatModel.stream(prompt);
    }

    @GetMapping("/prompt/chat3")
    public Flux<String> chat3(@RequestParam("question") String question) {
        //用户消息
        UserMessage userMessage = new UserMessage(question);
        //系统消息
        SystemMessage systemMessage = new SystemMessage("你是一个讲故事的助手,每个故事控制在300字以内");

        Prompt prompt = new Prompt(userMessage, systemMessage);

        return deepseekChatModel.stream(prompt).map(res->res.getResult().getOutput().getText());
    }

    @GetMapping("/prompt/chat4")
    public String chat4(@RequestParam("question") String question) {
        AssistantMessage assistantMessage = deepseekChatClient.prompt()
                .user(question)
                .call()
                .chatResponse()
                .getResult()
                .getOutput();
        return assistantMessage.getText();
    }
    
    @GetMapping("/prompt/chat5")
    public String chat5(@RequestParam("city") String city) {
        String answer = deepseekChatClient.prompt()
                .user(city + "未来3天天气情况如何?")
                .call()
                .chatResponse()
                .getResult()
                .getOutput()
                .getText();

        System.out.println("模型回答的天气:"+ answer);

        ToolResponseMessage toolResponseMessage = new ToolResponseMessage(
                List.of(new ToolResponseMessage.ToolResponse("1", "获得天气", city))
        );

        String toolResponse = toolResponseMessage.getText();

        return answer + toolResponse;
    }
}